package com.shisfish.library.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import org.apache.ibatis.annotations.Param;

import com.shisfish.library.Impl.InterestImpl;
import com.shisfish.library.Impl.UserInterestImpl;
import com.shisfish.library.Model.Interest;
import com.shisfish.library.Model.UserInterest;
import com.shisfish.library.Prere.Login;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;

@Path("userInterest")
public class UserInterestController extends GlobalVariable {

	private UserInterestImpl userInterestImpl = new UserInterestImpl();
	private InterestImpl interestImpl = new InterestImpl();
	
	@POST
	@Path("insertAUserInterest/{employee}")
	@Produces("application/json; charset=utf-8")
	public String insertAUserInterest(@PathParam("employee") String employee, @Param("interestStr") String[] interestStr, 
			@Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginUser(employee, request)) {
			String interests = "";
			StringBuffer sb1 = new StringBuffer();
			
			if (interestStr != null && interestStr.length > 0) {
				for (String string : interestStr) {
					sb1.append(string);
					sb1.append(",");
				}
			}
			if (sb1.length() > 0) {
				interests = sb1.substring(0, sb1.length() -1);
			}
			List<Interest> interests1 = interestImpl.selectType(1);
			if (interests1.size() > 0 && interests != null) {
				StringBuffer sb = new StringBuffer(",");
				
				for (Interest interest : interests1) {
					sb.append(interest.getId());
					sb.append(",");
				}
				String s = sb.toString();
				s = s.substring(0, s.length() - 1);
				
				interests += s;
			}
			
			if (interests.startsWith(",")) {
				interests = interests.substring(1);
			}
			
			List<UserInterest> userInterests = userInterestImpl.selectByKey(employee);
			
			if (userInterests.size() > 0) {
				UserInterest userInterest = userInterests.get(0);
				userInterest.setInterests(interests);
				int flag = userInterestImpl.updateOne(userInterest);
				if (flag == 0) {
					globalMap.put(STATUS_CODE, UPDATE_WR);
				} else {
					globalMap.put(STATUS_CODE, SUCCESS);
				}
			} else {
				UserInterest userInterest = new UserInterest(employee, interests, "", "");
				int flag = userInterestImpl.insertOne(userInterest);
				if (flag == 0) {
					globalMap.put(STATUS_CODE, INSERT_WR);
				} else {
					globalMap.put(STATUS_CODE, SUCCESS);
				}
			}
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("updateAUserInterest/{employee}")
	@Produces("application/json; charset=utf-8")
	public String updateAUserInterest(@PathParam("employee") String employee, @Param("interestStr") String[] interestStr, 
			@Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginUser(employee, request)) {
			List<UserInterest> userInterests = userInterestImpl.selectByKey(employee);
			String interests = "";
			StringBuffer sb1 = new StringBuffer();
			
			if (interestStr != null && interestStr.length > 0) {
				for (String string : interestStr) {
					sb1.append(string);
					sb1.append(",");
				}
			}
			if (sb1.length() > 0) {
				interests = sb1.substring(0, sb1.length() -1);
			}
			
			if (userInterests.size() > 0) {
				
				List<Interest> interests1 = interestImpl.selectType(1);
				
				if (interests == null) {
					interests = "";
				}
				
				if (interests1.size() > 0 && interests != null) {
					StringBuffer sb = new StringBuffer(",");
					
					for (Interest interest : interests1) {
						sb.append(interest.getId());
						sb.append(",");
					}
					String s = sb.toString();
					s = s.substring(0, s.length() - 1);
					
					interests += s;
				}
				
				if (interests.startsWith(",")) {
					interests = interests.substring(1);
				}
				
				UserInterest userInterest = userInterests.get(0);
				userInterest.setInterests(interests);
				int flag = userInterestImpl.updateOne(userInterest);
				if (flag == 0) {
					globalMap.put(STATUS_CODE, UPDATE_WR);
				} else {
					globalMap.put(STATUS_CODE, SUCCESS);
				}
			} else {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			}
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectByEmployee/{employee}")
	@Produces("application/json; charset=utf-8")
	public String selectByEmployee(@PathParam("employee") String employee) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
	
		List<UserInterest> userInterests = userInterestImpl.selectByKey(employee);
		if (userInterests.size() > 0) {
		
			UserInterest userInterest = userInterests.get(0);
			String interests = userInterest.getInterests();
			String[] inters = interests.split(",");
			
			List<Interest> interests1 = interestImpl.selectType(1);
			
			List<String> last = new ArrayList<String>();
			
			if (interests1.size() > 0) {
				String[] intes = new String[interests1.size()];
				
				for (int i = 0; i < interests1.size(); i++) {
					intes[i] = interests1.get(i).getId() + "";
				}
				
				for (int i = 0; i < inters.length; i++) {
					boolean bl = true;
					for (int j = 0; j < intes.length; j++) {
						if (inters[i].equals(intes[j])) {
							bl = false;
							
						}
					}
					if (bl) {
						last.add(inters[i]);
					}
					
				}
				
				List<Map<String, Object>> interestMap = new ArrayList<Map<String, Object>>();
				
				StringBuffer sb = new StringBuffer();
				for (String string : last) {
					String inter = interestImpl.selectNameById(Integer.parseInt(string));
					Map<String, Object> interMap = new HashMap<String, Object>();
					interMap.put(string, inter);
					interestMap.add(interMap);
					
					sb.append(string);
					sb.append(",");
				}
				
				String s = sb.toString();
				s = s.substring(0, s.length() - 1);
				
				userInterest.setInterests(s);
				
				globalMap.put("interests", interestMap);
				globalMap.put("obj", userInterest);
				globalMap.put(STATUS_CODE, SUCCESS);
				
			}
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
	
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectNotByEmployee/{employee}")
	@Produces("application/json; charset=utf-8")
	public String selectNotByEmployee(@PathParam("employee") String employee) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
	
		List<UserInterest> userInterests = userInterestImpl.selectByKey(employee);
		if (userInterests.size() > 0) {
		
			UserInterest userInterest = userInterests.get(0);
			String interests = userInterest.getInterests();
			String[] inters = interests.split(",");
			
			List<Interest> interests1 = interestImpl.selectType(0);
			
			List<String> last = new ArrayList<String>();
			
			if (interests1.size() > 0) {
				String[] intes = new String[interests1.size()];
				
				for (int i = 0; i < interests1.size(); i++) {
					intes[i] = interests1.get(i).getId() + "";
				}
				
				for (int i = 0; i < intes.length; i++) {
					boolean bl = false;
					for (int j = 0; j < inters.length; j++) {
						if (intes[i].equals(inters[j])) {
							bl = true;
						}
					}
					if (!bl) {
						last.add(intes[i]);
					}
					
				}
				
				List<Map<String, Object>> interestMap = new ArrayList<Map<String, Object>>();
				
				for (String string : last) {
					String inter = interestImpl.selectNameById(Integer.parseInt(string));
					Map<String, Object> interMap = new HashMap<String, Object>();
					interMap.put(string, inter);
					interestMap.add(interMap);

				}
				
				globalMap.put("interests", interestMap);
				globalMap.put(STATUS_CODE, SUCCESS);
				
			}
		} else {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
	
		return toJsonUtils.map2Json(globalMap);
	}
	
}
